package com.heima.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthPassDto;
import com.heima.model.admin.dtos.PageDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.user.mapper.AdUserMapper;
import com.heima.user.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/28
 */
@Service
public class AuthServiceImpl extends ServiceImpl<AdUserMapper,AdUser> implements AuthService {

    @Autowired
    private AdUserMapper adUserMapper;

    @Override
    public ResponseResult find(PageDto dto) {
        LambdaQueryWrapper<AdUser> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(dto.getStatus()!=null,AdUser::getStatus,dto.getStatus());
        IPage<AdUser> iPage= new Page<>(dto.getPage(),dto.getSize());
        queryWrapper.orderByDesc(AdUser::getCreatedTime);
        IPage<AdUser> adUserIPage = adUserMapper.selectPage(iPage, queryWrapper);
        List<AdUser> records = adUserIPage.getRecords();
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult authPass(AuthPassDto dto) {
        AdUser adUser = new AdUser();
        adUser.setId(dto.getId());
        adUser.setStatus((short) 9);
        adUserMapper.updateById(adUser);
        return ResponseResult.okResult("成功通过");
    }

    @Override
    public ResponseResult authFail(AuthPassDto dto) {
        AdUser adUser = new AdUser();
        adUser.setId(dto.getId());
        adUser.setStatus((short) 2);
        adUserMapper.updateById(adUser);
        return ResponseResult.okResult("成功驳回");
    }
}
